package org.eiahe.hr.employeeRelation.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.employeeRelation.domain.bo.ContractInfoBo;
import org.eiahe.hr.employeeRelation.domain.bo.ContractRenewalApplicationBo;
import org.eiahe.hr.employeeRelation.domain.vo.ContractInfoVo;
import org.eiahe.hr.employeeRelation.domain.vo.ContractRenewalApplicationVo;
import org.eiahe.hr.employeeRelation.service.IContractRenewalService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 合同续签控制器
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/employeeRelation/contractRenewal")
public class ContractRenewalController extends BaseController {

    private final IContractRenewalService contractRenewalService;

    /**
     * 查询合同信息列表
     */
    @SaCheckPermission("employeeRelation:contractRenewal:list")
    @GetMapping("/contract/list")
    public TableDataInfo<ContractInfoVo> listContract(ContractInfoBo bo, PageQuery pageQuery) {
        return contractRenewalService.selectContractInfoList(bo, pageQuery);
    }

    /**
     * 获取合同信息详细信息
     */
    @SaCheckPermission("employeeRelation:contractRenewal:query")
    @GetMapping(value = "/contract/{contractId}")
    public R<ContractInfoVo> getContractInfo(@PathVariable("contractId") Long contractId) {
        return R.ok(contractRenewalService.selectContractInfoById(contractId));
    }

    /**
     * 新增合同信息
     */
    @SaCheckPermission("employeeRelation:contractRenewal:add")
    @Log(title = "新增合同", businessType = BusinessType.INSERT,linkType = "renewal",nameField = "employeeNo")
    @PostMapping("/contract")
    public R<Void> addContract(@Validated(AddGroup.class) @RequestBody ContractInfoBo bo) {
        return toAjax(contractRenewalService.insertContractInfo(bo));
    }

    /**
     * 修改合同信息
     */
    @SaCheckPermission("employeeRelation:contractRenewal:edit")
    @Log(title = "修改合同", businessType = BusinessType.UPDATE,linkType = "renewal",nameField = "employeeNo")
    @PutMapping("/contract")
    public R<Void> editContract(@Validated(EditGroup.class) @RequestBody ContractInfoBo bo) {
        return toAjax(contractRenewalService.updateContractInfo(bo));
    }

    /**
     * 删除合同信息
     */
    @SaCheckPermission("employeeRelation:contractRenewal:remove")
    @Log(title = "删除合同", businessType = BusinessType.DELETE,linkType = "renewal",nameField = "employeeNo")
    @DeleteMapping("/contract/{contractIds}")
    public R<Void> removeContract(@PathVariable Long[] contractIds) {
        return toAjax(contractRenewalService.deleteContractInfoByIds(contractIds));
    }

    /**
     * 查询续签申请列表
     */
    @SaCheckPermission("employeeRelation:contractRenewal:list")
    @GetMapping("/application/list")
    public TableDataInfo<ContractRenewalApplicationVo> listApplication(ContractRenewalApplicationBo bo,
            PageQuery pageQuery) {
        return contractRenewalService.selectContractRenewalApplicationList(bo, pageQuery);
    }

    /**
     * 获取续签申请详细信息
     */
    @SaCheckPermission("employeeRelation:contractRenewal:query")
    @GetMapping(value = "/application/{applicationId}")
    public R<ContractRenewalApplicationVo> getApplicationInfo(@PathVariable("applicationId") Long applicationId) {
        return R.ok(contractRenewalService.selectContractRenewalApplicationById(applicationId));
    }

    /**
     * 新增续签申请
     */
    @SaCheckPermission("employeeRelation:contractRenewal:add")
    @Log(title = "新增续签申请", businessType = BusinessType.INSERT,linkType = "renewal",nameField = "employeeNo")
    @PostMapping("/application")
    public R<Void> addApplication(@Validated(AddGroup.class) @RequestBody ContractRenewalApplicationBo bo) {
        return toAjax(contractRenewalService.insertContractRenewalApplication(bo));
    }

    /**
     * 修改续签申请
     */
    @SaCheckPermission("employeeRelation:contractRenewal:edit")
    @Log(title = "修改续签申请", businessType = BusinessType.UPDATE,linkType = "renewal",nameField = "employeeNo")
    @PutMapping("/application")
    public R<Void> editApplication(@Validated(EditGroup.class) @RequestBody ContractRenewalApplicationBo bo) {
        return toAjax(contractRenewalService.updateContractRenewalApplication(bo));
    }

    /**
     * 删除续签申请
     */
    @SaCheckPermission("employeeRelation:contractRenewal:remove")
    @Log(title = "删除续签申请", businessType = BusinessType.DELETE,linkType = "renewal",nameField = "employeeNo")
    @DeleteMapping("/application/{applicationIds}")
    public R<Void> removeApplication(@PathVariable Long[] applicationIds) {
        return toAjax(contractRenewalService.deleteContractRenewalApplicationByIds(applicationIds));
    }

    /**
     * 审批通过续签申请
     * 审批通过后自动：
     * 1. 更新续签申请的审批信息
     * 2. 将旧合同标记为“已续签”
     * 3. 生成一份新合同记录
     */
    @SaCheckPermission("employeeRelation:contractRenewal:approve")
    @Log(title = "审批通过续签申请", businessType = BusinessType.UPDATE,linkType = "renewal",nameField = "employeeNo")
    @PostMapping("/application/approve")
    public R<Void> approveApplication(@RequestBody ContractRenewalApplicationBo bo) {
        return toAjax(contractRenewalService.approveContractRenewal(bo));
    }

    @SaCheckPermission("employeeRelation:contractRenewal:approve")
    @Log(title = "审批通过续签申请", businessType = BusinessType.UPDATE,linkType = "renewal",nameField = "employeeNo")
    @PostMapping("/application/reject")
    public R<Void> rejectApplication(@RequestBody ContractRenewalApplicationBo bo) {
        return toAjax(contractRenewalService.rejectContractRenewal(bo));
    }
}
